
var access_token = window.localStorage.getItem("token");
//get数据
$.ajax({
    type: "post",
    url: api('/user/view-horseman'),
    data: {
        access_token:access_token,
    },
    dataType: "json",
    async:false,
    cache:false,
    ifModified :true ,
    success: function (res) {
        if(res.success){
            var horesman = res.data.user.horseman_status
            if(horesman == 0){
                location.href = './login.html'
            }else if(horesman == 1){
                layer.open({
                    content: '你还未通过审核'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }else if(horesman == 3){
                layer.open({
                    content: '不允许访问'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }else{
                if(res.data.list ==''){
                    layer.open({
                        content: '没有分配路线'
                        ,skin: 'msg'
                        ,time: 2 //2秒后自动关闭
                    });
                }else {
                    horsemanRoute(res.data)
                    $('span').each(function (i,item) {
                        console.log(item.innerHTML)
                        if(item.innerHTML == ''){
                            console.log($(this).parents('.time'))
                            $(this).parents('.time').hide()
                        }
                    })
                }
            }
        }else {
            // console.log(access_token)
            layer.open({
                content: res.message +' <br> '+ access_token
                ,skin: 'msg'
                ,time: 5 //2秒后自动关闭
            });
        }
    }

});
//页面渲染
function horsemanRoute(man){
    console.log(man.title)
     var title = man.title
     var data = man.list
     var html = ''
     console.log(man.list)
     $.each(data,function(index,item){
         console.log(item.status_time)
         html += `<div class="content" data-id="${item.id}" onclick="paveHandle(this)">
    <div class="pull-left">
      <div>
        ${item.name}
      </div>
      <div>
        ${item.address}
      </div>
      <div class="time">
      处理时间:<span class="status_time">${item.status_time}</span>
      </div>
    </div>
    <div class="pull-rigth">
     ${item.status_text}
    </div>
</div>`
        $('.container').html(html)
         $('.title').text(title)
     })
}

//点击跳转
function paveHandle(e){
    var name = e.children[0].children[0].innerText
    var id = e.getAttribute("data-id")
    var user = {}
    user.id = id;
    user.name = name
    user.access_token = access_token
    window.localStorage.users = JSON.stringify(user)
    $.ajax({
        type: "post",
        url: api('/stock-bill/can-put'),
        data: {
            access_token:access_token,
            id:id,
        },
        dataType: "json",
        success:function (res) {
            console.log(res)
            if(res.success){
                window.location.href = './index.html?v='+ random +''
            }else {
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }
    })
}
